package com.nl.controller.xuyi.controller;


import com.github.pagehelper.PageInfo;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.xuyi.bean.XuYiOperatorInfo;
import com.nl.controller.web.global.GlobalConst;
import com.nl.controller.xuyi.service.XuYiOperatorService;
import com.nl.controller.web.util.FileUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @desc 运维人员控制层
 * @return null
 * @author zhaohz
 * @date 2020/9/8 17:21
 */
@RestController
@RequestMapping("xuYiOperator")
@Slf4j
public class XuYiOperatorController {

    @Autowired
    private XuYiOperatorService xuYiOperatorService;

    @ApiOperation(value = "运维人员", notes = "分页查询运维人员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "String"),
            @ApiImplicitParam(name = "operatorName", value = "运维人员名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "talkingCoding", value = "和对讲编码", required = true, dataType = "String")
    })
    @PostMapping("find")
    public Response findMgrTemplate(
            @RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "limit", required = true) int limit,
            @RequestParam(value = "operatorName", required = false) String operatorName,
            @RequestParam(value = "talkingCoding", required = false) String talkingCoding,
            @RequestParam Map<String, Object> paramMap
    ) {
        PageInfo<XuYiOperatorInfo> pageInfo = xuYiOperatorService.findOperator(page,limit,operatorName,talkingCoding);
        log.info("运维人员查询：{}", paramMap.toString());
        log.debug("运维人员查询统计返回数据：{}", pageInfo.getList().toString());
        return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
    }


    @ApiOperation(value = "修改运维人员信息表", notes = "插入及修改运维人员信息表")
    @PostMapping("configureOperator")
    public int configureOperator(
            @RequestParam(value = "operatorId", required = false) String operatorId,
            @RequestParam(value = "operatorName", required = false) String operatorName,
            @RequestParam(value = "operatorMsisdn", required = false) String operatorMsisdn,
            @RequestParam(value = "talkingCoding", required = false) String talkingCoding,
            @RequestParam(value = "flag", required = false) String flag,
            @RequestParam(value = "operatorChange", required = false) String operatorChange,
            HttpSession session, @RequestParam Map<String, Object> paramMap) {
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        /*
        模板状态正常
         */
        String operatorState = "0";
        String userMsisdn = user.getMsisdn();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = sdf.format(new Date());
        String modifyTime = sdf.format(new Date());
        log.info(createTime);
        int result = 0;
        result = xuYiOperatorService.configureOperator(flag,operatorId,operatorName,operatorMsisdn,talkingCoding,createTime,modifyTime,userMsisdn,operatorState,operatorChange);
        log.info("运维人员查询：{}", paramMap.toString());
        log.debug("运维人员查询返回数据：{}", result);
        return result;
    }


    @ApiOperation(value = "删除运维人员信息", notes = "删除运维人员信息")
    @PostMapping("remove")
    @ApiImplicitParam(name = "operatorId", value = "运维人员ID", required = true, dataType = "String")
    public int deleteOperator(@RequestParam(value = "operatorId") String operatorId) {
        int result= xuYiOperatorService.deleteOperator(operatorId);
        log.info("运维人员删除参数：{}", operatorId);
        log.debug("运维人员删除返回数据：{}", result);
        return result;
    }


    @ApiOperation(value = "查询单个短信模板", notes = "查询单个短信模板")
    @ApiImplicitParam(name = "operatorId", value = "短信模板编号", required = true, dataType = "String")
    @PostMapping("findByOperatorId")
    public XuYiOperatorInfo findOperatorByOperatorId(
            @RequestParam(value = "operatorId", required = true) String operatorId
    ) {
        XuYiOperatorInfo xuYiOperatorInfo = xuYiOperatorService.findOperatorByOperatorId(operatorId);
        log.info("查询单个运维人员信息：{}", operatorId);
        log.debug("查询单个运维人员修改返回数据：{}", xuYiOperatorInfo.toString());
        return xuYiOperatorInfo;
    }


    @ApiOperation(value = "模板下载", notes = "模板下载")
    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void download(HttpServletResponse res) {
        String fileName = "operator.csv";
        FileUtil.downloadOperator(fileName, res);
    }

    /**
     * @desc 处理文件上传
     * @param file
     * @param request
     * @return com.nl.controller.common.model.Response
     * @author zhaohz
     * @date 2020/9/8 17:23
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Response uploadOperator(@RequestParam("file") MultipartFile file,
                              HttpServletRequest request) {

        SmsUsers user = (SmsUsers) request.getSession().getAttribute("user");
        String useMsisdn = user.getMsisdn();
        String now = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        String fileName = user.getUserId() + now + "operator.csv";
        String path = GlobalConst.FILE_STATION_PATH;
        try {
            /*
            文件上传
             */
            FileUtil.fileUpload(file, request, path, fileName);
            log.info("文件上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("文件上传异常");
            return ResponseUtil.error(1, "文件上传异常");
        }
        try {
            /*
            文件解析
             */
            int successNum = xuYiOperatorService.readCsv(path + "/" + fileName ,useMsisdn);
            return  ResponseUtil.sucess(successNum);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("数据异常请检查");
            return ResponseUtil.error(-1,"系统异常");
        }
    }
}
